<?php

use RH\Helper\DateTime;
?>
<?php
//var_dump($this->post);
//var_dump($this->employes);
//foreach ($this->templates as $tpl) {
//    var_dump($tpl);
//}
$post		 = $this->post;
$site		 = $this->site;
$employeList = $this->employes;
$employes = array();
$tplList	 = $this->templates;
// Parcours de la liste des Plannings type à copier
$templates	 = array();
foreach ($tplList as $tpl) {
    $templates[$tpl->employe_id] = $tpl;
}
$codes	 = array();
$codec	 = array();
foreach ($this->codes as $code) {
    $codec[$code->code_activite_id]	 = $code->code;
    $codes[]			 = $code;
}
// Template Vide
$empty = new \RH\Model\PlanningTemplate();
?>
<div class="content-head">
    <form action="#" method="post" id="tpl-empty">
	<label>
	    <span style='padding: auto 5px;'>Libellé du Planning Type </span><input type='text' name='libelle' value='Planning Type <?php echo $site->libelle ?>' size="100">
	</label>
	<?php foreach ($employeList as $e) : ?>
	    <?php $employes[]	 = $e ?>
	    <?php $tpl = $templates[$e->employe_id] ?>
    	<input type="hidden" id="employe-<?php echo $e->employe_id ?>" name="employe[<?php echo $e->employe_id ?>]" value="<?php echo $e->employe_id ?>">

    	<input type="hidden" id="lundi-<?php echo $e->employe_id ?>" name="lundi-<?php echo $e->employe_id ?>" value="<?php echo $tpl->lundi ?>">
    	<input type="hidden" id="lundi-<?php echo $e->employe_id ?>_entree" name="lundi-<?php echo $e->employe_id ?>_entree" value="<?php echo $tpl->lundi_entree ?>">
    	<input type="hidden" id="lundi-<?php echo $e->employe_id ?>_sortie" name="lundi-<?php echo $e->employe_id ?>_sortie" value="<?php echo $tpl->lundi_sortie ?>">

    	<input type="hidden" id="mardi-<?php echo $e->employe_id ?>" name="mardi-<?php echo $e->employe_id ?>" value="<?php echo $tpl->mardi ?>">
    	<input type="hidden" id="mardi-<?php echo $e->employe_id ?>_entree" name="mardi-<?php echo $e->employe_id ?>_entree" value="<?php echo $tpl->mardi_entree ?>">
    	<input type="hidden" id="mardi-<?php echo $e->employe_id ?>_sortie" name="mardi-<?php echo $e->employe_id ?>_sortie" value="<?php echo $tpl->mardi_sortie ?>">

    	<input type="hidden" id="mercredi-<?php echo $e->employe_id ?>" name="mercredi-<?php echo $e->employe_id ?>" value="<?php echo $tpl->mercredi ?>">
    	<input type="hidden" id="mercredi-<?php echo $e->employe_id ?>_entree" name="mercredi-<?php echo $e->employe_id ?>_entree" value="<?php echo $tpl->mercredi_entree ?>">
    	<input type="hidden" id="mercredi-<?php echo $e->employe_id ?>_sortie" name="mercredi-<?php echo $e->employe_id ?>_sortie" value="<?php echo $tpl->mercredi_sortie ?>">

    	<input type="hidden" id="jeudi-<?php echo $e->employe_id ?>" name="jeudi-<?php echo $e->employe_id ?>" value="<?php echo $tpl->jeudi ?>">
    	<input type="hidden" id="jeudi-<?php echo $e->employe_id ?>_entree" name="jeudi-<?php echo $e->employe_id ?>_entree" value="<?php echo $tpl->jeudi_entree ?>">
    	<input type="hidden" id="jeudi-<?php echo $e->employe_id ?>_sortie" name="jeudi-<?php echo $e->employe_id ?>_sortie" value="<?php echo $tpl->jeudi_sortie ?>">

    	<input type="hidden" id="vendredi-<?php echo $e->employe_id ?>" name="vendredi-<?php echo $e->employe_id ?>" value="<?php echo $tpl->vendredi ?>">
    	<input type="hidden" id="vendredi-<?php echo $e->employe_id ?>_entree" name="vendredi-<?php echo $e->employe_id ?>_entree" value="<?php echo $tpl->vendredi_entree ?>">
    	<input type="hidden" id="vendredi-<?php echo $e->employe_id ?>_sortie" name="vendredi-<?php echo $e->employe_id ?>_sortie" value="<?php echo $tpl->vendredi_sortie ?>">

    	<input type="hidden" id="samedi-<?php echo $e->employe_id ?>" name="samedi-<?php echo $e->employe_id ?>" value="<?php echo $tpl->samedi ?>">
    	<input type="hidden" id="samedi-<?php echo $e->employe_id ?>_entree" name="samedi-<?php echo $e->employe_id ?>_entree" value="<?php echo $tpl->samedi_entree ?>">
    	<input type="hidden" id="samedi-<?php echo $e->employe_id ?>_sortie" name="samedi-<?php echo $e->employe_id ?>_sortie" value="<?php echo $tpl->samedi_sortie ?>">

    	<input type="hidden" id="dimanche-<?php echo $e->employe_id ?>" name="dimanche-<?php echo $e->employe_id ?>" value="<?php echo $tpl->dimanche ?>">
    	<input type="hidden" id="dimanche-<?php echo $e->employe_id ?>_entree" name="dimanche-<?php echo $e->employe_id ?>_entree" value="<?php echo $tpl->dimanche_entree ?>">
    	<input type="hidden" id="dimanche-<?php echo $e->employe_id ?>_sortie" name="dimanche-<?php echo $e->employe_id ?>_sortie" value="<?php echo $tpl->dimanche_sortie ?>">
	<?php endforeach ?>
    </form>
	<div class = 'toolbar'>
	    <button type = 'button' class = 'btn btn-success' onclick = '$("#tpl-empty1").submit();'>
		Enregistrer
	    </button>
	    <button type = 'button' class = 'btn btn-inverse' onclick = '$("#pl-dialog").dialog("close"); return false;'>
		Annuler
	    </button>
	</div>
</div>

<div class="content-body">
    <div class="list-container">
	<table class="pl-table" style='width: 100%'>
	    <thead>
		<tr>
		    <td style='text-align: center' rowspan='2'>
			<span>Mat.</span>
		    </td>
		    <td style='text-align: center' rowspan='2'>
			<span>Poste</span>
		    </td>
		    <td style='text-align: center' rowspan='2'>
			<span>Nom</span>
		    </td>
		    <td style='text-align: center' colspan='3'>
			Lundi
		    </td>
		    <td style='text-align: center' colspan='3'>
			Mardi
		    </td>
		    <td style='text-align: center' colspan='3'>
			Mercredi
		    </td>
		    <td style='text-align: center' colspan='3'>
			Jeudi
		    </td>
		    <td style='text-align: center' colspan='3'>
			Vendredi
		    </td>
		    <td style='text-align: center' colspan='3'>
			Samedi
		    </td>
		    <td style='text-align: center' colspan='3'>
			Dimanche
		    </td>
		</tr>
		<tr style='font-size: 0.7em'>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		    <td>Code</td>
		    <td>Entrée</td>
		    <td>Sortie</td>

		</tr>
	    </thead>
	    <tbody style="font-size: 0.9em; text-align: center; width: 100%">
		<?php
		$jaune = 1;
		?>
		<?php foreach ($employes as $e): ?>
    		<tr>
		    <td class="<?php echo (($jaune % 2) == 0) ? 'color-jaune2' : 'color-jaune1' ?>">
			    <?php echo $e->employe_id ?>
    		    </td>
    		    <td class="<?php echo (($jaune % 2) == 0) ? 'color-jaune2' : 'color-jaune1' ?>">
			    <?php echo $e->poste_code ?>
    		    </td>
    		    <td class="<?php echo (($jaune % 2) == 0) ? 'color-jaune2' : 'color-jaune1' ?>">
			    <?php echo $e->prenom ?>
    		    </td>

			<?php $tpl = $templates[$e->employe_id] ?>

    		    <!-- Lundi -->
    		    <td class="color-<?php echo $codec[$tpl->lundi] ?>" id="tpl-<?php echo $e->employe_id ?>-lundi" style="position: relative">
			<a href="#" class='full-link pl-day-edit code_activite' data-id='<?php echo $e->employe_id ?>' data-prefix='lundi' data-pk='<?php echo $e->employe_id ?>'>
				<?php echo $codec[$tpl->lundi] ?>
    			</a>
		    </td>
		    <td class="color-<?php echo $codec[$tpl->lundi] ?>" id="tpl-<?php echo $e->employe_id ?>-lundi-entree">
			<a href="#" class="full-link pl-day-edit entree" data-id="<?php echo $e->employe_id ?>" data-prefix="lundi" data-pk="<?php echo $e->employe_id ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->lundi_entree); ?>
    			</a>
    		    </td>
		    <td class="color-<?php echo $codec[$tpl->lundi] ?>" id="tpl-<?php echo $e->employe_id ?>-lundi-sortie">
			<a href="#" class="full-link pl-day-edit sortie" data-id="<?php echo $e->employe_id ?>" data-prefix="lundi" data-pk="<?php echo $e->employe_id ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->lundi_entree); ?>
    			</a>
		    </td>

    		    <!-- Mardi -->
    		    <td class="color-<?php echo $codec[$tpl->mardi] ?>" id="tpl-<?php echo $e->employe_id ?>-mardi" style="position: relative">
			<a href="#" class='full-link pl-day-edit code_activite' data-id='<?php echo $e->employe_id ?>' data-prefix='mardi' data-pk='<?php echo $e->employe_id ?>'>
				<?php echo $codec[$tpl->mardi] ?>
    			</a>
    		    </td>
    		    <td class="color-<?php echo $codec[$tpl->mardi] ?>" id="tpl-<?php echo $e->employe_id ?>-mardi-entree">
			<a href="#" class="full-link pl-day-edit entree" data-id="<?php echo $e->employe_id ?>" data-prefix="mardi" data-pk="<?php echo $e->employe_id ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->mardi_entree); ?>
    			</a>
    		    </td>
    		    <td class="color-<?php echo $codec[$tpl->mardi] ?>" id="tpl-<?php echo $e->employe_id ?>-mardi-sortie">
			<a href="#" class="full-link pl-day-edit sortie" data-id="<?php echo $e->employe_id ?>" data-prefix="mardi" data-pk="<?php echo $e->employe_id ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->mardi_entree); ?>
    			</a>
    		    </td>

    		    <!-- Mercredi -->
    		    <td class="color-<?php echo $codec[$tpl->mercredi] ?>" id="tpl-<?php echo $e->employe_id ?>-mercredi" style="position: relative">
			<a href="#" class='full-link pl-day-edit code_activite' data-id='<?php echo $e->employe_id ?>' data-prefix='mercredi' data-pk='<?php echo $e->employe_id ?>'>
				<?php echo $codec[$tpl->mercredi] ?>
    			</a>
    		    </td>
    		    <td class="color-<?php echo $codec[$tpl->mercredi] ?>" id="tpl-<?php echo $e->employe_id ?>-mercredi-entree">
			<a href="#" class="full-link pl-day-edit entree" data-id="<?php echo $e->employe_id ?>" data-prefix="mercredi" data-pk="<?php echo $e->employe_id ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->mercredi_entree); ?>
    			</a>
    		    </td>
    		    <td class="color-<?php echo $codec[$tpl->mercredi] ?>" id="tpl-<?php echo $e->employe_id ?>-mercredi-sortie">
			<a href="#" class="full-link pl-day-edit sortie" data-id="<?php echo $e->employe_id ?>" data-prefix="mercredi" data-pk="<?php echo $e->employe_id ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->mercredi_entree); ?>
    			</a>
    		    </td>

    		    <!-- Jeudi -->
    		    <td class="color-<?php echo $codec[$tpl->jeudi] ?>" id="tpl-<?php echo $e->employe_id ?>-jeudi" style="position: relative">
			<a href="#" class='full-link pl-day-edit code_activite' data-id='<?php echo $e->employe_id ?>' data-prefix='jeudi' data-pk='<?php echo $e->employe_id ?>'>
				<?php echo $codec[$tpl->jeudi] ?>
    			</a>
    		    </td>
    		    <td class="color-<?php echo $codec[$tpl->jeudi] ?>" id="tpl-<?php echo $e->employe_id ?>-jeudi-entree">
			<a href="#" class="full-link pl-day-edit entree" data-id="<?php echo $e->employe_id ?>" data-prefix="jeudi" data-pk="<?php echo $e->employe_id ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->jeudi_entree); ?>
    			</a>
    		    </td>
    		    <td class="color-<?php echo $codec[$tpl->jeudi] ?>" id="tpl-<?php echo $e->employe_id ?>-jeudi-sortie">
			<a href="#" class="full-link pl-day-edit sortie" data-id="<?php echo $e->employe_id ?>" data-prefix="jeudi" data-pk="<?php echo $e->employe_id ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->jeudi_entree); ?>
    			</a>
    		    </td>

    		    <!-- Vendredi -->
    		    <td class="color-<?php echo $codec[$tpl->vendredi] ?>" id="tpl-<?php echo $e->employe_id ?>-vendredi" style="position: relative">
			<a href="#" class='full-link pl-day-edit code_activite' data-id='<?php echo $e->employe_id ?>' data-prefix='vendredi' data-pk='<?php echo $e->employe_id ?>'>
				<?php echo $codec[$tpl->vendredi] ?>
    			</a>
    		    </td>
    		    <td class="color-<?php echo $codec[$tpl->vendredi] ?>" id="tpl-<?php echo $e->employe_id ?>-vendredi-entree">
			<a href="#" class="full-link pl-day-edit entree" data-id="<?php echo $e->employe_id ?>" data-prefix="vendredi" data-pk="<?php echo $e->employe_id ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->vendredi_entree); ?>
    			</a>
    		    </td>
    		    <td class="color-<?php echo $codec[$tpl->vendredi] ?>" id="tpl-<?php echo $e->employe_id ?>-vendredi-sortie">
			<a href="#" class="full-link pl-day-edit sortie" data-id="<?php echo $e->employe_id ?>" data-prefix="vendredi" data-pk="<?php echo $e->employe_id ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->vendredi_entree); ?>
    			</a>
    		    </td>

    		    <!-- Samedi -->
    		    <td class="color-<?php echo $codec[$tpl->samedi] ?>" id="tpl-<?php echo $e->employe_id ?>-samedi" style="position: relative">
			<a href="#" class='full-link pl-day-edit code_activite' data-id='<?php echo $e->employe_id ?>' data-prefix='samedi' data-pk='<?php echo $e->employe_id ?>'>
				<?php echo $codec[$tpl->samedi] ?>
    			</a>
    		    </td>
    		    <td class="color-<?php echo $codec[$tpl->samedi] ?>" id="tpl-<?php echo $e->employe_id ?>-samedi-entree">
			<a href="#" class="full-link pl-day-edit entree" data-id="<?php echo $e->employe_id ?>" data-prefix="samedi" data-pk="<?php echo $e->employe_id ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->samedi_entree); ?>
    			</a>
    		    </td>
    		    <td class="color-<?php echo $codec[$tpl->samedi] ?>" id="tpl-<?php echo $e->employe_id ?>-samedi-sortie">
			<a href="#" class="full-link pl-day-edit sortie" data-id="<?php echo $e->employe_id ?>" data-prefix="samedi" data-pk="<?php echo $e->employe_id ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->samedi_entree); ?>
    			</a>
    		    </td>

    		    <!-- Dimanche -->
    		    <td class="color-<?php echo $codec[$tpl->dimanche] ?>" id="tpl-<?php echo $e->employe_id ?>-dimanche" style="position: relative">
			<a href="#" class='full-link pl-day-edit code_activite' data-id='<?php echo $e->employe_id ?>' data-prefix='dimanche' data-pk='<?php echo $e->employe_id ?>'>
				<?php echo $codec[$tpl->dimanche] ?>
    			</a>
    		    </td>
    		    <td class="color-<?php echo $codec[$tpl->dimanche] ?>" id="tpl-<?php echo $e->employe_id ?>-dimanche-entree">
			<a href="#" class="full-link pl-day-edit entree" data-id="<?php echo $e->employe_id ?>" data-prefix="dimanche" data-pk="<?php echo $e->employe_id ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->dimanche_entree); ?>
    			</a>
    		    </td>
    		    <td class="color-<?php echo $codec[$tpl->dimanche] ?>" id="tpl-<?php echo $e->employe_id ?>-dimanche-sortie">
			<a href="#" class="full-link pl-day-edit sortie" data-id="<?php echo $e->employe_id ?>" data-prefix="dimanche" data-pk="<?php echo $e->employe_id ?>">
				<?php echo DateTime::timeWithNoSecond($tpl->dimanche_entree); ?>
    			</a>
    		    </td>

    		</tr>
		    <?php $jaune++ ?>
		<?php endforeach; ?>
	    </tbody>
	</table>
    </div>
</div>

<script type="text/javascript">
	    // Initialisation des liens d'édition
	    var openTemplate = '';
	    $(document).ready(function() {
		// pl-day-edit
		$('.pl-day-edit').click(function(Event) {
		    Event.preventDefault();

		    var prefix = $(this).attr('data-prefix');
		    var pk = $(this).attr('data-pk');
		    var id = $(this).attr('data-id');

		    if (openTemplate != '') {
			// Il y a un pop up ouvert, on le ferme
			$('#pl-edit form').hide('slow');
			$('.pl-popin').remove();
			//alert(openTemplate  + ' is Removed');

			if (openTemplate == '#tpl-' + pk + '-' + prefix) {
			    // On quitte
			    //alert(openTemplate  + ' is Closed');
			    openTemplate = '';

			    return false;
			}
			openTemplate = '';
		    }

		    var ajax = $.ajax({
			type: 'post',
			url: "<?php echo $this->url('RH/default', array('controller'	 => 'template', 'action'	 => 'templateempty')) ?>",
			data: "&prefix=" + prefix + "&pk=" + pk + "&id=" + id
		    });
		    ajax.success(function(data) {
			$('#tpl-' + pk + '-' + prefix).append(data);
			openTemplate = '#tpl-' + pk + '-' + prefix;
//		alert(openTemplate  + ' is now Open');
			$('#pl-edit form').show('slow');
		    });
		    ajax.error(function() {
			alert('erreur');
		    });
		});
	    });
</script>

<style type="text/css">
    pl-table{
        border-collapse: collapse;
    }
    .pl-table tr,
    .pl-table td{
        border: 1px solid #DFDFDF;
        padding: 4px 2px;
    }
    .pl-table thead tr{
        background: #95B3D7;
    }
    .pl-table thead td{
        text-align: center;
        min-height: 1.5em;
    }
    .pl-table tbody tr:nth-child(even){
        background: #f8f8f8;
    }
    .color-jaune1{
        background: #fff67f;
    }
    .color-jaune2{
        background: #ffee05;
    }
    .color-jaune3{
        background: #ffe84c;
    }
<?php foreach ($codes as $code): ?>
            .color-<?php echo $code->code ?>{ background: #<?php echo $code->couleur ?>}
<?php endforeach ?>
</style>